package com.example.asus.myapplication;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;

import java.util.ArrayList;

/**
 * Created by asus on 2019/4/7.
 */

public class MyActitvity extends AppCompatActivity{
    ArrayList<News> newsArrayList;
    ListView mLvFoods;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化数据
        initData();
        //初始化界面
        initView();
        //设置事件
        initEvent();
    }

    private void initEvent() {
        mLvFoods.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public int position;


            @Override
                                            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                                                Toast.makeText(MyActitvity.this,""+position,Toast.LENGTH_SHORT).show();

                                                //跳转到新的Activity，显示position位置详细信息
                                                Intent intent=new Intent(MyActitvity.this,DatallActivity.class);


                                                //携带数据
                                                intent.putExtra("position",position);
                                                intent.putExtra("id",newsArrayList.get(position).getIcon());
                                                intent.putExtra("name",newsArrayList.get(position).getName());
                                                intent.putExtra("price",newsArrayList.get(position).getPrice());
                                                intent.putExtra("desc",newsArrayList.get(position).getDesc());
                                                intent.putExtra("pic",newsArrayList.get(position).getPic());
                                                 intent.putExtra("hot",newsArrayList.get(position).getHot());
                                                       intent.putExtra("sour",newsArrayList.get(position).getSour());
                                                          intent.putExtra("Vegetable",newsArrayList.get(position).getVegetable());
                                                                  intent.putExtra("meat",newsArrayList.get(position).getMeat());
                //跳转
                //跳转
                                                //跳转
                                                startActivity(intent);


                                            }
                                        }
        );
    }


    private void initView() {
        mLvFoods=findViewById(R.id.lv_news);
        MyBaseAdapter adapter=new MyBaseAdapter();
        mLvFoods.setAdapter(adapter);

    }

    private void initData() {
        String str =readNewsJsonFile();
        newsArrayList=parseJSON(str);

    }

    private ArrayList<News> parseJSON(String str) {



        //创建链表
        ArrayList<News> newsList=new ArrayList<>();
        try {
            JSONArray jsonArray=new JSONArray(str);
            for (int i=0;i<jsonArray.length();i++){
                JSONObject jsonObject =jsonArray.getJSONObject(i);
                //创建news对象
                Foods foods=new foods();
                mLvFoods.setId();

                Food.setId(jsonObject.optString("id"));
                foods.setName(jsonObject.optString("name"));
                foods.setPrice(jsonObject.optString("price"));
                foods.setDesc(jsonObject.optString("desc"));
                foods.setPic(jsonObject.optString(" pic"));
                foods.setHot(jsonObject.optString("hot"));
                foods.setSour(jsonObject.optString("sour"));
                foods.setVegetable(jsonObject.optString("vegetable"));
                foods.setMeat(jsonObject.optString("meat"));

                //将news添加到arrsylist中
                newsList.add(foods);
            }

        } catch (JSONException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }


        return  newsList;

    }

    private String readNewsJsonFile() {
        String str="";
        //构建输入流
        //
        InputStream inputStream= getResources().openRawResource(R.raw.foods);
        //读取数据
        try {
            //定义一个字节数组用来保存输入流中读取的字节信息



            byte[] bytes =new  byte[inputStream.available()];
            inputStream.read(bytes);
            inputStream.close();
            str =new String(bytes);
            return str;

        } catch (IOException e) {
            e.printStackTrace();
            return str;
        }



    }
    class MyBaseAdapter extends BaseAdapter{



        @Override
        public int getCount() {
            return newsArrayList.size();
        }


        @Override
        public Object getItem(int position) {
            return newsArrayList.get(position);

        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            //从项目布局创建view
            View view=  View.inflate(MainActivity.this,R.layout.list_item,null);

            ImageView icon =view.findViewById(R.id.iv_icon);
            //修改当前数据项的图片名称
            //获取图片控件显示icon的图片



            String pic=newsArrayList.get(position).getId();
            //根据文件名，获取资源ｉｄ
            int mipmap =getResources().getIdentifier(pic.substring(0,pic.indexOf(".")),"mipmap",getPackageName());


            icon.setImageResource(mipmap);
            //从项目布局视图中获取标题文本控件


            TextView tvTitle=view.findViewById(R.id.food_title);
            //获取当前新闻的标题，并在tvTitle显示


            tvTitle.setText(newsArrayList.get(position).getTitle());
            //返回view



            return view;
        }
    }




}
